package com.example.designer.chain.manage;

import com.example.designer.chain.ChainHandler;
import org.springframework.stereotype.Component;

import java.util.LinkedList;
import java.util.List;

/**
 * 管理责任链表 : 将各个节点使用链表形势连接起来
 */
@Component
public class HandlerManageMent implements ChainHandler {

    //存储链表
    private List<ChainHandler> list = new LinkedList<>();

    //索引
    private int index = 0;

    //添加责任对象
    public HandlerManageMent add(ChainHandler chainHandler){
        list.add(chainHandler);
        return this;
    }

    @Override
    public void doSomething(Integer inPut, ChainHandler chain) {
        //遍历完成 return
        if (index == list.size()) return;
        //获取当前责任对象
        ChainHandler chainHandler = list.get(index);
        //修改索引值 已获取下一个责任对象
        index++;
        chainHandler.doSomething(inPut,chain);
    }
}
